// pages/search2/search2.js
const util = require('../../utils/util.js')
import Bmob from '../../utils/dist/Bmob-1.7.0.min.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    showModal: false,
    showModal2: false,
    showModal3: false,
    showModal4: false,
    showModal5: false,
    shuju:[],
    img:[]

  },

  previewImage: function (e) {   //图片显示
    wx.previewImage({
      current: e.target.dataset.src,
      urls: this.data.img
    })
  },

  previewImage1: function (e) {    //图片显示
    wx.previewImage({
      current: e.target.dataset.src,
      urls: this.data.shuju.pic
    })
  },

  funSave: function () {    //保存

    let current = Bmob.User.current();
    const query = Bmob.Query('_User');
    query.select("savenumber");
    query.get(current.objectId).then(res => {
      console.log(res)
      this.setData({
        savenumber: res.savenumber,
      })
      if (this.data.savenumber >= 5) {
        console.log("对不起您已经有5条的保存了")
        this.setData({
          showModal3: true,
          showModal5: true,
        })
      } else {

        const query = Bmob.Query('savedata');
        query.equalTo("who", "==", current.objectId);    //检查我的菜单里是否有发布过这条菜单
        query.select("dataid");      //单条数据查询，用id来识别是否有此条菜单
        query.find().then(res => {
          console.log(res, "5")
          var dataids = []
          for (var i = 0; i < res.length; i++) {    //提取res里的dataid数值到dataids数组里
            dataids.push(
              res[i].dataid
            )
          }
          //console.log(typeof this.data.id)
          var judge = dataids.indexOf(Number(this.data.id))    //判断dataids数组里是否有当前菜品的id,如果有则judge为返回下标，无则返回-1
          if (judge == -1) {      //用户没有保存过，所以执行保存代码
            console.log(judge, "78")
            console.log(dataids)
            const pointer = Bmob.Pointer('_User')      //数据关联
            const poiID = pointer.set(current.objectId)

            query.set("who", poiID) //数据关联
            query.set("dataid", Number(this.data.shuju.dataid))  //Number为将this.data.id转换为Number型
            //console.log(typeof this.data.id)
            //console.log(typeof current.objectId)
            query.set("pic", this.data.shuju.pic)
            query.set("name", this.data.shuju.name)
            query.set("time", this.data.shuju.time)
            query.set("people", this.data.shuju.people)
            query.save().then(res => {
              console.log(res)
              const query = Bmob.Query('_User')
              //console.log(current.objectId,"1")        //计数，每当用户点击一次保存则增加一次
              query.get(current.objectId).then(res => {
                res.increment('savenumber', 1)
                res.save()

                wx.showToast({          //保存成功提示图
                  title: '保存成功',
                  icon: 'success',
                  duration: 3000
                });

              }).catch(err => {
                console.log(err)
              })
            }).catch(err => {
              console.log(err)
            })
          } else {
            console.log("你已经保存过了！")
            this.setData({
              showModal: true,
              showModal2: true,
            })


          }

        });
      }
      console.log(this.data.savenumber)
    }).catch(err => {
      console.log(err)
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shuju=[]
    var img = []
    var shuju = JSON.parse(options.shuju);
   // console.log(options.shuju,"45")
    //var shuju = options.shuju
    this.setData({
      shuju:shuju
    })
    for (var i = 0; i < shuju.process.length;i++){
      img.push(shuju.process[i].pic)
    }
    this.setData({
      img:img
    })
    console.log(img,"45")
    console.log(shuju,"55")
    console.log(typeof(shuju))
    
  },
  ok1: function () {
    this.setData({
      showModal3: false,
      showModal4: false,
      showModal5: false,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
  }
})